<?php

   function getDBConnection() {
      $host = "localhost";
      $user = "postgres";
      $pass = "azulgrana";
      $db   = "asistencia_db";

      $con = pg_connect("host=$host dbname=$db user=$user password=$pass")
         or die ("Could not connect to server\n");
      return $con;
   }

   function select($select) {
      $con = getDBConnection();
      pg_query("BEGIN") or die("Could not start transaction\n");
      $res = pg_query($select);

      if (!$res) die("Error occured in query statement\n");
      else pg_query("COMMIT") or die("Transaction commit failed\n");

      $result = array();
      while ($data = pg_fetch_object($res))
         array_push($result,$data);

      pg_close($con);

      return $result;
   }

   function execute_statement($statement) {
      $con = getDBConnection();
      pg_query("BEGIN") or die("Could not start transaction\n");
      $res = pg_query($statement);

      if ($res) {
         echo "Commiting transaction\n";
         pg_query("COMMIT") or die("Transaction commit failed\n");
      } else {
         echo "Rolling back tranbsaction\n";
         pg_query("ROLLBACK") or die("Transaction rollback failed\n");
      }
      pg_close($con);
   }


?>
